﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FonShare.Classes.Data;
using FonShare.Classes;

namespace FonShare
{
    public partial class CreateGroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MessageLabel.Text = "";
            }
        }

        protected void CreateButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(GroupnameTextBox.Text))
            {
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                if (dc.FS_Groups.Where(record => record.GroupName == GroupnameTextBox.Text).Count() == 0)
                {
                    FS_Group newGroup = new FS_Group();
                    newGroup.GroupName = GroupnameTextBox.Text;
                    newGroup.OwnerID = (int) Session[FS_Constants.Session.USERID];
                    dc.FS_Groups.InsertOnSubmit(newGroup);
                    dc.SubmitChanges();
                    MessageLabel.Text = "Groupname created.";
                }
                else
                {
                    MessageLabel.Text = "Sorry, this groupname already exists, choose another one.";
                }
            }
            else
            {
                MessageLabel.Text = "Groupname can not be empty.";
            }

        }
    }
}
